import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
part 'dialog.dart';

showAlert({
  final String? title,
  final String? msg,
  final String? cancelText = '取消',
  final String? sureText = '确定',
  final Color? cancelColor,
  final Color? sureColor,
  final VoidCallback? cancelAction,
  final VoidCallback? sureAction,
}) {
  if (Get.context == null) {
    return;
  }
  List<Widget> _getActions() {
    List<Widget> actions = [];
    if (cancelText != null) {
      actions.add(
        CupertinoDialogAction(
          onPressed: () {
            Navigator.of(Get.context!).pop();
            if (cancelAction != null) {
              Future.delayed(const Duration(milliseconds: 300), () {
                cancelAction();
              });
            }
          },
          child: Text(
            cancelText,
            style: TextStyle(
                color: cancelColor ?? const Color(0xff999999), fontSize: 13.sp),
          ),
        ),
      );
    }
    if (sureText != null) {
      actions.add(
        CupertinoDialogAction(
          onPressed: () {
            Navigator.of(Get.context!).pop();
            if (sureAction != null) {
              Future.delayed(const Duration(milliseconds: 300), () {
                sureAction();
              });
            }
          },
          child: Text(
            sureText,
            style: TextStyle(
                color: cancelColor ?? const Color(0xff43a8e8), fontSize: 13.sp),
          ),
        ),
      );
    }
    return actions;
  }

  showCupertinoDialog(
      context: Get.context!,
      barrierDismissible: true,
      builder: (context) {
        return CupertinoAlertDialog(
          title: title == null
              ? null
              : Text(
                  title,
                  style: TextStyle(
                      color: const Color(0xff333333),
                      fontSize: 18.sp,
                      fontWeight: FontWeight.bold),
                ),
          content: msg == null
              ? null
              : Text(
                  msg,
                  textAlign: TextAlign.left,
                  style: TextStyle(
                      color: const Color(0xff666666), fontSize: 15.sp),
                ),
          actions: _getActions(),
        );
      });
}

/*
class BottomSheetController extends GetxController {
  // List<int>? choosedIndexs;
}

class BottomSheetView extends StatelessWidget {
  BottomSheetView({
    Key? key,
    required this.titles,
    this.title,
    this.choosedIndexs,
    this.isMutiableChoose = false,
    this.onTap,
  }) : super(key: key);
  final List<String> titles;
  List<int>? choosedIndexs;
  String? title;
  bool isMutiableChoose = false;
  Function(List<int>)? onTap;

  final controller = Get.put(BottomSheetController());

  @override
  Widget build(BuildContext context) {
    // controller.choosedIndexs = choosedIndexs;
    // var _choosedIndexs = controller.choosedIndexs;
    return GetBuilder<BottomSheetController>(builder: (controller) {
      return Container(
        // borderRadius: BorderRadius.circular(10.r),

        child: Column(
          children: [
            Container(
              height: 50.r,
              alignment: Alignment.center,
              decoration: const BoxDecoration(
                  border: Border(
                      bottom: BorderSide(color: Color(0xffeaeaea), width: 1))),
              child: Text(
                title ?? '请选择',
                style: pfSCStyle(color: ColorsUtil.title, fontSize: 16.sp),
              ),
            ),
            Container(
              constraints: BoxConstraints(
                  maxHeight: 5 * 50.r + SizeFit.safeBottom + 100.r,
                  minHeight: 100.r),
              child: ListView(
                shrinkWrap: true,
                children: titles
                    .asMap()
                    .map(
                      (key, value) => MapEntry(
                        key,
                        GestureDetector(
                            onTap: () {
                              if (isMutiableChoose) {
                                if (choosedIndexs?.contains(key) == true) {
                                  choosedIndexs!.remove(key);
                                } else {
                                  if (choosedIndexs == null) {
                                    choosedIndexs = [key];
                                  } else {
                                    choosedIndexs!.add(key);
                                  }
                                }

                                controller.update();
                              } else {
                                Navigator.of(context).pop();
                                if (onTap != null) {
                                  onTap!([key]);
                                }
                              }
                            },
                            child: Row(
                              children: [
                                Container(
                                  height: 50.r,
                                  width: double.infinity,
                                  decoration: const BoxDecoration(
                                      border: Border(
                                          bottom: BorderSide(
                                              color: Color(0xffeeeeee),
                                              width: 1))),
                                  alignment: Alignment.center,
                                  child: Text(
                                    value,
                                    style: TextStyle(
                                        color:
                                            choosedIndexs?.contains(key) == true
                                                ? ColorsUtil.red
                                                : const Color(0xff333333),
                                        fontSize: 14.sp),
                                  ),
                                ),
                                isMutiableChoose &&
                                        choosedIndexs?.contains(key) == true
                                    ? Icon(
                                        Icons.check,
                                        color: ColorsUtil.green,
                                      )
                                    : SizedBox(),
                              ],
                            )),
                      ),
                    )
                    .values
                    .toList(),
              ),
            ),
            SafeArea(
              child: Container(
                height: 50.r,
                alignment: Alignment.center,
                decoration: const BoxDecoration(
                  border: Border(
                    bottom: BorderSide(color: Color(0xffeaeaea), width: 1),
                    top: BorderSide(color: Color(0xffeaeaea), width: 1),
                  ),
                ),
                child: Text(
                  '取消',
                  style: pfSCStyle(color: ColorsUtil.title, fontSize: 16.sp),
                ),
              ),
            )
          ],
        ),
      );
    });
  }
}

 */